﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Web.Catalog;

public partial class sysadmin_UserManager_SetDepartmentAttributes :PowerCreator.LMS.Web.Base.BasePage
{
    Department opDepart = new Department();
    Catalog opCatalog = new Catalog();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();

            if (Request.QueryString["DepartmentID"] != null)
            {
                string departmentid = Request.QueryString["DepartmentID"].ToString();
                ViewState["DepartmentID"] = departmentid;
                VOTDepartment vo = opDepart.Load(int.Parse(departmentid));
                txt_DepartmentName.Text = vo.DepartmentName;
                txt_UserCount.Text = "0";
                txt_UserCount.Enabled = false;
            }

            BinderCatalog();
            txt_DepartmentName.ReadOnly = true;
        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_SetDepartmentAttributes_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_SetDepartmentAttributes_ClassName.Text = GetLanageValueByKey("lbl_SetDepartmentAttributes_ClassName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_SetDepartmentAttributes_SpecialtyName.Text = GetLanageValueByKey("lbl_SetDepartmentAttributes_SpecialtyName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_SetDepartmentAttributes_EnrollmentTime.Text = GetLanageValueByKey("lbl_SetDepartmentAttributes_EnrollmentTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_SetDepartmentAttributes_FinishTime.Text = GetLanageValueByKey("lbl_SetDepartmentAttributes_FinishTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_SetDepartmentAttributes_ClassEstate.Text = GetLanageValueByKey("lbl_SetDepartmentAttributes_ClassEstate", PowerCreator.LMS.Language.LanagetItemType.Page);
       // lbl_SetDepartmentAttributes_ClassPrincipal.Text = GetLanageValueByKey("lbl_SetDepartmentAttributes_ClassPrincipal", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_SetDepartmentAttributes_ClassNumber.Text = GetLanageValueByKey("lbl_SetDepartmentAttributes_ClassNumber", PowerCreator.LMS.Language.LanagetItemType.Page);
       // lab_StartTime_css.Text = GetLanageValueByKey("depratment_attributes_timeformation", PowerCreator.LMS.Language.LanagetItemType.Page);
       // lab_EndTime_css.Text = GetLanageValueByKey("depratment_attributes_timeformation", PowerCreator.LMS.Language.LanagetItemType.Page);

        rfv_txt_DepartmentName.ErrorMessage = GetLanageValueByKey("depratment_attributes_classname", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txt_StartTime.ErrorMessage = GetLanageValueByKey("depratment_attributes_classstrattime", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txt_EndTime.ErrorMessage = GetLanageValueByKey("depratment_attributes_classendtime", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        rdbtn_DeptattriState0.Text = GetLanageValueByKey("depratment_attributes_classstate0", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_DeptattriState1.Text = GetLanageValueByKey("depratment_attributes_classstate1", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Save.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderCatalog()
    { 
        //绑定专业
        string search = "";
        DataTable dt = opCatalog.QueryCatalog(search);
        ArrayList list = new ArrayList();
        list = opCatalog.GetCatalogTree(dt);
        ddl_Catalog.DataSource = list;
        ddl_Catalog.DataTextField = "Text";
        ddl_Catalog.DataValueField = "Value";
        ddl_Catalog.DataBind();
    }
    private bool CheckPageData()
    {
        string search = string.Format("DepartmentName = '{0}' and DepartmentId <> {1}", txt_DepartmentName.Text, ViewState["DepartmentID"]);

        if (opDepart.Count(search) != 0)
        {
            //lbl_msg_description.Text = "存在相同的部门名称或班级名称!请重新输入班级名称!";
            lbl_msg_description.Text = GetLanageValueByKey("depratment_attributes_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        if (!TimeUtils.IsDateTime(txt_StartTime.Text.Trim()))
        {
            //lbl_msg_description.Text = "开始时间数据输入格式不对，请检查！";
            lbl_msg_description.Text = GetLanageValueByKey("depratment_attributes_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        if (!TimeUtils.IsDateTime(txt_EndTime.Text.Trim()))
        {
            //lbl_msg_description.Text = "毕业时间数据输入格式不对，请检查！";
            lbl_msg_description.Text = GetLanageValueByKey("depratment_attributes_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        if (TimeUtils.TimeCompart(DateTime.Parse(txt_StartTime.Text), DateTime.Parse(txt_EndTime.Text)).Seconds > 0)
        {
            //lbl_msg_description.Text = "入学时间必须大于毕业时间!";
            lbl_msg_description.Text = GetLanageValueByKey("depratment_attributes_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        return true;
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        
        VOTDepartment voDepartment = opDepart.Load(int.Parse(ViewState["DepartmentID"].ToString()));
        if (voDepartment != null)
        {
            VOTDepartmentAttribute vo = new VOTDepartmentAttribute();
            vo.DeptattriEndTime = DateTime.Parse(txt_EndTime.Text);
            vo.DeptattriStartTime = DateTime.Parse(txt_StartTime.Text);
            vo.DeptattriState = false;
            if (rdbtn_DeptattriState1.Checked)
                vo.DeptattriState = true;
            vo.FKDeptattriCatalogId = int.Parse(ddl_Catalog.SelectedValue.ToString());
            vo.FKDeptattriDepartmentID = voDepartment.DepartmentId;
            vo.DeptattriTeacherID = 0;
            vo.DeptattriStudentNum = 0;
            int id=0;
            if (opDepart.AddDepartAttributes(vo,ref id))
            {
                voDepartment.DepartmentName = txt_DepartmentName.Text;
                if (opDepart.Update(voDepartment))
                {
                    string url = "Department.aspx";
                    Script.RedirectURL(url);
                }
            }
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "Department.aspx";
        Script.RedirectURL(url);
    }
}
